package cn.pet.service.manage.controller;

import cn.pet.service.manage.entity.PetUser;
import cn.pet.service.manage.entity.api.Result;
import cn.pet.service.manage.entity.dto.UserDTO;
import cn.pet.service.manage.entity.vo.UserVO;
import cn.pet.service.manage.mapstruct.UserMapstruct;
import cn.pet.service.manage.service.PetUserService;
import cn.pet.service.manage.utils.SecurityUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author zwr
 * @version 1.0
 * @description TODO
 * @date 2023/2/18 11:40
 */
@RestController
@RequestMapping("/manage/employee")
@Slf4j
@AllArgsConstructor
public class EmployeeController {

    private final PetUserService petUserService;

    /**
     * 查看员工列表
     * @param userDTO
     * @return
     */
    @GetMapping("list")
    public Object list(UserDTO userDTO){
        if (log.isDebugEnabled()){
            log.debug("查询会员列表：第{}页，每页条数：{}",userDTO.getPageNum(),userDTO.getPageSize());
        }
        //构造分页构造器
//        Page pageInfo = new Page(userDTO.getPage(),userDTO.getPageSize());
        PageHelper.startPage(userDTO.getPageNum(),userDTO.getPageSize());
        //构造条件构造器
        LambdaQueryWrapper<PetUser> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.eq(PetUser::getUserType,2);
        queryWrapper.like(StringUtils.isNotEmpty(userDTO.getUsername()),PetUser::getUsername,userDTO.getUsername());
        //添加排序条件
        queryWrapper.orderByDesc(PetUser::getCreateTime);
        //执行查询
//        petUserService.page(pageInfo, queryWrapper);
        return Result.ok("success",new PageInfo<>(petUserService.list(queryWrapper)));
    }

    /**
     * 添加员工
     * @param petUser
     * @return
     */
    @PostMapping("add")
    public Object add(@RequestBody PetUser petUser){
        petUser.setPassword(SecurityUtil.encryptPassword(petUser.getPassword()));
        petUser.setUserType(2);
        if (!petUserService.save(petUser)){
            return Result.error("添加失败");
        }
        return Result.ok("添加成功");
    }

    /**
     * 修改用户状态
     * @return
     */
    @PutMapping("enableUserStatus")
    public Object enableUserStatus(@RequestBody UserDTO userDTO){
        PetUser petUser = new PetUser();
        petUser.setId(userDTO.getId());
        petUser.setStatus(userDTO.getStatus());
        if (!petUserService.updateById(petUser)){
            return Result.error(userDTO.getStatus()==1 ? "禁用失败" : "启用失败");
        }
        return Result.ok(userDTO.getStatus()==1 ? "禁用成功" : "启用成功");
    }

    /**
     * 编辑用户
     * @param petUser
     * @return
     */
    @PutMapping("edit")
    public Object edit(@RequestBody PetUser petUser){
        if (petUser.getPassword()!=null){
            petUser.setPassword(SecurityUtil.encryptPassword(petUser.getPassword()));
        }
        if (!petUserService.updateById(petUser)){
            return Result.error("编辑失败");
        }
        return Result.ok("编辑成功");
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") Integer id){
        if (!petUserService.removeById(id)){
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }

    /**
     * 查看个人信息
     * @param id
     * @return
     */
    @GetMapping("detail/{id}")
    public Object detail(@PathVariable("id") Integer id){
        PetUser petUser = petUserService.getById(id);
        UserVO userVO = new UserVO();
        userVO = UserMapstruct.INSTANCE.userToUserVo(petUser,userVO);
        return Result.ok("success",userVO);
    }

}
